// 更新管理员密码
$("#updateAdminPwdBtn").on("click", function () {
    var userId = $(".userId").val();
    var userName = $(".userName").val();
    var userPassword = $(".oldUserPassword").val();
    var newUserPassword = $(".newUserPassword").val();
    var checkNewUserPassword = $(".checkNewUserPassword").val();
    var reg_userPassword = /^([0-9]|[a-zA-Z]){6,16}$/;      // 密码
    if (userPassword != null && userPassword.length != 0 && reg_userPassword.test(userPassword)) {
        if (newUserPassword != null && newUserPassword.length != 0 && reg_userPassword.test(newUserPassword)) {
            if (checkNewUserPassword != null && checkNewUserPassword.length != 0 && reg_userPassword.test(checkNewUserPassword)) {
                if (newUserPassword == checkNewUserPassword) {
                    $.ajax({
                        type: "POST",
                        url: "/adminManage/checkAdminPassword",
                        data: {userId: userId, userName: userName, userPassword: userPassword},
                        dataType: "json",
                        success: function (data) {
                            if (data.result == "success") {
                                $.ajax({
                                    type: "POST",
                                    url: "/adminManage/updateAdminPassword",
                                    data: {userId: userId, userName: userName, userPassword: newUserPassword},
                                    dataType: "json",
                                    success: function (data) {
                                        if (data.result == "success") {
                                            $(".password-message").html("密码过期，3秒后退出管理系统").css({"color": "green"});
                                            setTimeout(function () {
                                                window.location.href = "/logout";
                                            }, 3000);
                                        }
                                    },
                                    error: function () {
                                        $(".password-message").html("ajax 服务器错误").css({"color": "red"});
                                    }
                                });
                            } else{
                                $(".password-message").html("原密码错误").css({"color": "red"});
                            }
                        },
                        error: function () {
                            $(".password-message").html("ajax 服务器错误").css({"color": "red"});
                        }
                    });
                } else {
                    $(".password-message").html("两次输入的密码不一致").css({"color": "red"});
                }
            } else {
                $(".password-message").html("确认密码不能为空，且满足6~16位数字、字母").css({"color": "red"});
            }
        } else {
            $(".password-message").html("新密码不能为空，且满足6~16位数字、字母").css({"color": "red"});
        }
    } else {
        $(".password-message").html("旧密码不能为空，且满足6~16位数字、字母").css({"color": "red"});
    }
});

// 更新管理员信息
$("#adminInfoBtn").on("click", function () {
    var userId = $(".userId").val();
    var userName = $(".userName").val();
    var userEmail = $(".userEmail").val();
    var userPhone = $(".userPhone").val();
    var reg_userPhone = /^1[345678]\d{9}$/;                 // 电话
    var reg_userEmail =/^\w+@\w+(\.[a-zA-Z]{2,3}){1,2}$/;   // 邮箱
    if (userName != null && userName.length != 0) {
        if (userEmail != null && userEmail.length != 0 && reg_userEmail.test(userEmail)) {
            if (userPhone != null && userPhone.length != 0 && reg_userPhone.test(userPhone)) {
                $.ajax({
                    type: "POST",
                    url: "/adminManage/checkAdminRepeat",
                    data: {userId: userId, userName: userName, userEmail: userEmail},
                    dataType: "json",
                    success: function (data) {
                        if (data.result == null) {
                            $.ajax({
                                type: "POST",
                                url: "/adminManage/updateAdmin",
                                data: $("#adminInfoForm").serialize(),
                                dataType: "json",
                                success: function (data) {
                                    if (data.result == "success") {
                                        $(".base-info-message").html("更新成功").css({"color": "green"});
                                    } else {
                                        $(".base-info-message").html("更新失败").css({"color": "green"});
                                    }
                                },
                                error: function () {
                                    $(".base-info-message").html("ajax 服务器错误").css({"color": "red"});
                                }
                            });
                        } else {
                            $(".base-info-message").html(data.result).css({"color": "red"});
                        }
                    },
                    error: function () {
                        $(".base-info-message").html("服务器错误，操作失败").css({"color": "red"});
                    }
                });
            } else {
                $(".base-info-message").html("电话号码不能为空，支持11位手机号码").css({"color": "red"});
            }
        } else {
            $(".base-info-message").html("邮箱不能为空，格式：123Test@qq.com").css({"color": "red"});
        }
    } else {
        $(".base-info-message").html("用户名不能为空").css({"color": "red"});
    }
});